package cn.weekimwee.firstlineofcodeforkotlin.unit11

import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import cn.weekimwee.firstlineofcodeforkotlin.BaseActivity
import cn.weekimwee.firstlineofcodeforkotlin.R
import com.baidu.location.BDLocation
import com.baidu.location.LocationClient
import com.baidu.location.LocationClientOption
import kotlinx.android.synthetic.main.activity_unit11.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

class Unit11Activity : BaseActivity() {
    private lateinit var locationClient: LocationClient
    private var lat :Double = 0.0
    private var lng :Double = 0.0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_unit11)
        locationClient = LocationClient(applicationContext)
        locationClient.locOption = LocationClientOption().apply { setIsNeedAddress(true) }
        locationClient.registerLocationListener { location ->
            runOnUiThread {
                position.text = StringBuilder().apply {
                    lat = location.latitude
                    lng = location.longitude
                    append("纬度：${location.latitude}\n经度：${location.longitude}\n")
                    append("国家：${location.country}\n省：${location.province}\n市：${location.city}\n区：${location.district}\n街道：${location.street}\n")
                    val type = when (location.locType) {
                        BDLocation.TypeGpsLocation -> "gps"
                        BDLocation.TypeNetWorkLocation -> "网络"
                        else -> "位置"
                    }
                    append("定位方式:$type")
                }
            }
        }

        val permissionList = mutableListOf<String>()
        if (ContextCompat.checkSelfPermission(this@Unit11Activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION)
        }
        if (ContextCompat.checkSelfPermission(this@Unit11Activity, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            permissionList.add(Manifest.permission.READ_PHONE_STATE)
        }
        if (ContextCompat.checkSelfPermission(this@Unit11Activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
        }
        if (permissionList.isNotEmpty()) {
            ActivityCompat.requestPermissions(this@Unit11Activity, permissionList.toTypedArray(), 1)
        } else {
            requestLocation()
        }

        startMap.setOnClickListener {
            startActivity<MapActivity>("lat" to lat,"lng" to lng)
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        when (requestCode) {
            1 -> {
                if (grantResults.isNotEmpty()) {
                    grantResults.forEach {
                        if(it!=PackageManager.PERMISSION_GRANTED){
                            toast("必须同意所有权限才能使用本程序")
                            finish()
                            return
                        }
                    }
                    requestLocation()
                } else {
                    toast("不知道发生了啥")
                    finish()
                }
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        locationClient.stop()
    }

    private fun requestLocation() {
        locationClient.start()
    }
}
